package home_pagePO;

import com.sun.org.apache.xerces.internal.util.EntityResolverWrapper;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import java.security.PublicKey;

public class Account_ListPO {
    AppiumDriver driver;

    public Account_ListPO(AppiumDriver driver) {
        this.driver = driver;
    }

    public static By tv_title = By.id("com.ksbao.nursingstaffs:id/tv_title");
    public static int X = 442;
    public static int Y = 301;
    //当前页面左边列表
    public static By left_list = By.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View[1]/android.view.View/android.widget.TextView");

//    //用来判断右边一级目录是否存在
//    public static By right_list_data = By.id("com.ksbao.nursingstaffs:id/rv_right");

    //用来获取右边目录当前页面展示有多少个
    public static By getRight_list = By.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View[2]/android.widget.LinearLayout");

    //用来判断右边二级目录是否存在
    public static By tow_List = By.id("com.ksbao.nursingstaffs:id/rv_subject");
    //用来获取二级目录当前页面展示有多少个
    public static By getTow_List = By.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.View/android.widget.LinearLayout");

    public Account_ListPO account_listPO() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, 5, (long) 0.5);
        //顶部切换科目
        driver.findElement(tv_title).click();
        Thread.sleep(2000);
        TouchAction touchAction = new TouchAction(driver);
        //坐标点击
        touchAction.tap(PointOption.point(X, Y)).perform();
        Thread.sleep(1000);

        //得到1-当前页面可选择的随机数
        int ran_left = 2 + (int) (Math.random() * driver.findElements(left_list).size());
        //随机选择当前页面左边列表
        By getLeft = By.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View[1]/android.view.View[" + ran_left + "]/android.widget.TextView");
        //获取将点击的左边列表职称名称
        String text = driver.findElement(getLeft).getText();
        System.out.println("选择职称:" + text);
        //随机选择左边列表
        driver.findElement(getLeft).click();
        Thread.sleep(500);


        //得到当前右边列表个数
        int one_size = driver.findElements(getRight_list).size();
        System.out.println("一级目录获取到：" + one_size + "科目");
        int ran_right = 1 + (int) (Math.random() * driver.findElements(getRight_list).size());
        //用来获取当前页面展示，右边点击+点击的名字
        By getRight = By.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View[2]/android.widget.LinearLayout[" + ran_right + "]/android.widget.TextView");
        String getRight_Text = driver.findElement(getRight).getText();
        System.out.println("选择了" + getRight_Text + "科目");
        driver.findElement(getRight).click();
        Thread.sleep(1000);


        new SearchPO(driver);

        if (check(driver, tow_List)) {
            int tow_size = driver.findElements(getTow_List).size();
            System.out.println("二级目录获取到：" + tow_size + "科目");
            //用来随机获取当前页面的科目
            int ran_right_tow = 1 + (int) (Math.random() * driver.findElements(getTow_List).size());
            By get_Right_Tow = By.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.View/android.widget.LinearLayout[" + ran_right_tow + "]/android.widget.TextView");
            String text1 = driver.findElement(get_Right_Tow).getText();
            System.out.println("选择了:" + text1 + "科目");
            driver.findElement(get_Right_Tow).click();


            extracted(wait);
            String home_title = driver.findElement(SearchPO.title_text).getText();
            System.out.println("当前首页显示的科目:\t" + home_title);
            try {
                Assert.assertEquals(home_title, text1);
                System.out.println("选择成功，进到首页");
            } catch (AssertionError e) {
                System.out.println("预期与实际不符合" + e);
            }
        } else {
            extracted(wait);
            //首页广告关闭
            //等待首页广告弹窗出现且关闭
            String home_title = driver.findElement(SearchPO.title_text).getText();
            System.out.println("当前首页显示的科目:\t" + home_title);
            try {
                Assert.assertEquals(home_title, getRight_Text);
                System.out.println("选择成功，进到首页");
            } catch (AssertionError e) {
                System.out.println("预期与实际不符合" + e);
            }
        }

        return new Account_ListPO(driver);

    }

    private void extracted(WebDriverWait wait) {
        wait.until(ExpectedConditions.presenceOfElementLocated((SearchPO.advertisement))).click();
    }

    //判断页面元素是否存在
    public Boolean check(WebDriver driver, By seletor) {
        try {
            driver.findElement(seletor);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

